new Vue({
    el: '#app',
    data() {
        return {
            IO_form: {
                Data: 'employee',//状态
                page: 1,			//页数
                limit: 10,			//每页数据条数
                Enterprise_Info_SerialNumber: $.cookie('enterprise'),
            },
            Employees: [
                // {
                //     text: '组织1',
                //     value: "1",
                //     children: [{
                //         text: '部门1',
                //         value: '1-1',
                //         children:[{
                //             text: '员工',
                //             value:'01'
                //         }]
                //     }]
                // },
                // {
                //     text: '组织2',
                //     value: "2",
                //     children: [{
                //         text: '部门1',
                //         value: '2-1'
                //     }]
                // },
                // {
                //     text: '组织3',
                //     value: "3",
                //     children: [{
                //         text: '部门1',
                //         value: '3-1'
                //     }]
                // },
            ],
            formData: {},
            IA_tableData: [{
                Organization_Name: "",
                Department_Name: "",
                Employee_Phone: "",
            }],		//表格数据
            II_total: 1,
            dialogForm: false,
            title: '',
            multipleSelection: [],
            data: [{
                label: '一级 1',
                children: [{
                    label: '二级 1-1',
                    children: [{
                        label: '三级 1-1-1'
                    }]
                }]
            }, {
                label: '一级 2',
                children: [{
                    label: '二级 2-1',
                    children: [{
                        label: '三级 2-1-1'
                    }]
                }, {
                    label: '二级 2-2',
                    children: [{
                        label: '三级 2-2-1'
                    }]
                }]
            }, {
                label: '一级 3',
                children: [{
                    label: '二级 3-1',
                    children: [{
                        label: '三级 3-1-1'
                    }]
                }, {
                    label: '二级 3-2',
                    children: [{
                        label: '三级 3-2-1'
                    }]
                }]
            }],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            IB_disabled: true,
            dialogVisible: false,
            selectedRow: {},
            treeProps: {
                label: 'label', // 对应节点的显示文本
                value: 'value', // 对应节点的值
                children: 'children' // 对应子节点的属性名
            },
            selectedData: [],
            roles: [],
            selectedRoles: [],
            Enterprise_id: ''
        }
    },
    mounted() {
        // this.getList(this.IO_form)
        this.getEmployees()
        this.getRoles()
    },
    methods: {
        showRoleConfigDialog(row) {
            this.selectedRow = row;
            // this.selectedRoles = row.roles || [];
            this.roles.forEach((item1) => {
                this.$set(item1, 'isChecked', false)
            })
            row.roles.forEach(item => {
                this.roles.forEach((item1) => {
                    if (item.Role_ID == item1.Role_Id) {
                        this.$set(item1, 'isChecked', true)
                        // item1.isChecked=true
                        console.log(item1, '看看啥样');
                    }
                })
            })
            this.$nextTick(() => {
                this.dialogVisible = true;
            })
            this.selectedRoles = row.roles ? row.roles.map(role => role) : [];
            // this.selectedRoles = this.roles.filter(role => role.isChecked);
            console.log('this.roles', this.roles);
            console.log('this.selectedRoles', this.selectedRoles);
            console.log('row', row);
            console.log('row.roles', row.roles);
        },
        handleCheckboxChange(index) {
            // 保持 selectedRoles 最新
            this.roles[index].isChecked = !this.roles[index].isChecked;
            // console.log('this.role[index].isChecked', this.role[index].isChecked);
        },
        handleNodeClick(data) {
            console.log(data);
        },
        handleSizeChange(val) {
            this.IO_form.page = 1;
            this.IO_form.limit = val;
            this.getList(this.IO_form)
        },
        //号码按键查询
        handleCurrentChange(val) {
            this.IO_form.page = val;
            this.getList(this.IO_form)
        },
        Addemployee() {
            window.location.href = './Add.html'
        },
        getEmployees() {
            const this_ = this
            $.ajax({
                type: 'GET',
                url: `${BASE_URL}/User/getUserBelongEnterprise?enterpriseName=${$.cookie('enterprise')=='1'?'':decodeURI($.cookie('enterpriseName'))}`,
                success: function (res) {
                    res = res.filter(item => item.Enterprise_Info_SerialNumber!='1')
                    console.log(res)
                    this_.Employees = res.map(item => {
                        return {
                            label: item.Enterprise_Name,
                            value: item.Enterprise_Info_SerialNumber,
                            children: item.Data.map(child => ({
                                label: child.Nick,
                                value: item.Enterprise_Info_SerialNumber,
                                User_Number: child.Member_SerialNumber,
                                roles: child.Role_List
                            }))
                        };
                    });
                }
            })
        },
        // getList(Arg) {     //换页方法
        //     let LF_this = this
        //     $.ajax({
        //         type: 'POST',
        //         url: DomainName + "/getEmployeeAllAction?token=" + $.cookie('token'),
        //         data: Arg,
        //         complete: function (XMLHttpRequest) {
        //             if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
        //                 window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
        //             }
        //         },
        //         success: function (Result) {
        //             console.log(Result);
        //             Result.tableData.forEach(item => {
        //                 item.Employee_Phone = item.Employee_Phone.slice(0, 3) + '****' + item.Employee_Phone.slice(7, 11)
        //             })
        //             LF_this.IA_tableData = Result.tableData      //将获取的数据存到表格数据中
        //             LF_this.II_total = Number(Result.limit)
        //         },
        //         dataType: "json"
        //     });
        // },
        getRoles() {
            console.log('222');
            const _this = this;
            $.ajax({
                url: BASE_URL+'/weixin/getRoleInfo',
                type: 'GET',
                success: function (res) {
                    console.log('角色', res);
                    _this.roles = res.data
                    _this.roles.forEach(item => {
                        item.isChecked = false
                    })
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试");
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            });
        },
        handleCheckChange(e) {
            console.log('e', e)
            this.Enterprise_id = e.value;
            this.selectedData = e.children || [e];
        },
        saveRoles() {
            const _this = this
            const selectedRoles = this.roles.filter((role) => role.isChecked);
            console.log('11111', _this.selectedRoles);
            const Role_List = selectedRoles.map(item => ({
                Role_Id: item.Role_Id,
                // 假设roles数组是全局的，通过roleId找到对应的Role_Name
                Role_Name: item.Role_Name
            }));
            let data = {
                User_Id: _this.selectedRow.User_Number,
                Enterprise_No: _this.Enterprise_id,
                Role_List: Role_List
            };
            console.log('data', data)
            // $.ajax({
            //     type: 'POST',
            //     url: BASE_URL+'/weixin/setUserRoleInfo',
            //     data: JSON.stringify(data),
            //     contentType: 'application/json',
            //     success: function (res) {
            //         console.log(res);
            //         if (res === 'success') {
            //             _this.$message.success("保存成功");
            //             location.reload();
            //         } else {
            //             _this.$message.error("保存失败，请稍后再试");
            //         }
            //     },
            //     error: function (res) {
            //         console.log(res);
            //         _this.$message.error("操作失败，请稍后再试");
            //     },
            //     complete: function (XMLHttpRequest) {
            //         _this.loading = false;
            //         if (XMLHttpRequest.getResponseHeader("REDIRECT") === "REDIRECT") {
            //             window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            //         }
            //     }
            // });
            $.ajax({
                url: DomainName+'/addRoleInfo',
                type: 'POST',
                data:{
                  Data:JSON.stringify(data),
                  URL:'/weixin/setUserRoleInfo'
                },
                success: function (res) {
                  console.log(res);
                  if (res.Status == 'success') {
                    _this.$message.success("提交成功")
                    window.history.go(-1)
                  } else {
                    _this.$message.error("提交失败，请稍后再试")
                  }
                },
                error: function () {
                  _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                  _this.loading = false
                  if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                    window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                  }
                }
              })
            console.log("Saving roles for employee:", data);
            // _this.dialogVisible = false;
            // _this.getEmployees();
            // _this.selectedData = []
        }
    },
})